<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="&amp;#x524D;&amp;#x8A00;&amp;#x7F51;&amp;#x4E0A;&amp;#x6709;&amp;#x5F88;&amp;#x591A;&amp;#x4EBA;&amp;#x5206;&amp;#x4EAB;&amp;#x4E86;&amp;#x81EA;&amp;#x5DF1;&amp;#x7684;&amp;#x5B66;&amp;#x4E60;&amp;#x8FC7;&amp;#x7A0B;&amp;#xFF0C;&amp;#x4E0D;&amp;#x8FC7;&amp;#x81EA;&amp;#x5DF1;&amp;#x505A;&amp;#x5206;">
<meta property="og:type" content="article">
<meta property="og:title" content="第一篇博客:YYModel解析（1）">
<meta property="og:url" content="http://yoursite.com/2016/09/26/yymodel解析1/index.html">
<meta property="og:site_name" content="Motom 的 秘密树屋">
<meta property="og:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x7F51;&amp;#x4E0A;&amp;#x6709;&amp;#x5F88;&amp;#x591A;&amp;#x4EBA;&amp;#x5206;&amp;#x4EAB;&amp;#x4E86;&amp;#x81EA;&amp;#x5DF1;&amp;#x7684;&amp;#x5B66;&amp;#x4E60;&amp;#x8FC7;&amp;#x7A0B;&amp;#xFF0C;&amp;#x4E0D;&amp;#x8FC7;&amp;#x81EA;&amp;#x5DF1;&amp;#x505A;&amp;#x5206;">
<meta property="og:image" content="http://images.cnitblog.com/blog/292994/201303/06190150-6424732a31c74ceaaf1c4077b3b9b7b9.png">
<meta property="og:updated_time" content="2016-09-28T03:29:32.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第一篇博客:YYModel解析（1）">
<meta name="twitter:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x7F51;&amp;#x4E0A;&amp;#x6709;&amp;#x5F88;&amp;#x591A;&amp;#x4EBA;&amp;#x5206;&amp;#x4EAB;&amp;#x4E86;&amp;#x81EA;&amp;#x5DF1;&amp;#x7684;&amp;#x5B66;&amp;#x4E60;&amp;#x8FC7;&amp;#x7A0B;&amp;#xFF0C;&amp;#x4E0D;&amp;#x8FC7;&amp;#x81EA;&amp;#x5DF1;&amp;#x505A;&amp;#x5206;">
<meta name="twitter:image" content="http://images.cnitblog.com/blog/292994/201303/06190150-6424732a31c74ceaaf1c4077b3b9b7b9.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://yoursite.com/2016/09/26/yymodel解析1/"/>

  <title> 第一篇博客:YYModel解析（1） | Motom 的 秘密树屋 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Motom 的 秘密树屋</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                第一篇博客:YYModel解析（1）
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-26T08:48:54+08:00" content="2016-09-26">
              2016-09-26
            </time>
          </span>

          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="&#x524D;&#x8A00;"><a href="#&#x524D;&#x8A00;" class="headerlink" title="&#x524D;&#x8A00;"></a>&#x524D;&#x8A00;</h1><p>&#x7F51;&#x4E0A;&#x6709;&#x5F88;&#x591A;&#x4EBA;&#x5206;&#x4EAB;&#x4E86;&#x81EA;&#x5DF1;&#x7684;&#x5B66;&#x4E60;&#x8FC7;&#x7A0B;&#xFF0C;&#x4E0D;&#x8FC7;&#x81EA;&#x5DF1;&#x505A;&#x5206;&#x6790;&#x8BB0;&#x5FC6;&#x4F1A;&#x6DF1;&#x523B;&#x4E00;&#x70B9;&#xFF0C;so &#xFF0C;&#x4E0B;&#x9762;&#x5C31;&#x5F00;&#x59CB;&#x4E00;&#x6B65;&#x4E00;&#x6B65;&#x5206;&#x6790;&#x3002;</p>
<h1 id="part-one"><a href="#part-one" class="headerlink" title="part one"></a>part one</h1><h2 id="static-void-SimpleObjectExample"><a href="#static-void-SimpleObjectExample" class="headerlink" title="static void SimpleObjectExample"></a>static void SimpleObjectExample</h2><pre><code>static void NestObjectExample() {
    YYRepo *repo = [YYRepo modelWithJSON:@&quot;         \
    {                                               \
        \&quot;rid\&quot;: 123456789,                         \
        \&quot;name\&quot;: \&quot;YYKit\&quot;,                        \
        \&quot;createTime\&quot; : \&quot;2011-06-09T06:24:26Z\&quot;,  \
        \&quot;owner\&quot;: {                                \
            \&quot;uid\&quot; : 989898,                       \
            \&quot;name\&quot; : \&quot;ibireme\&quot;                  \
        } \
    }&quot;];
    NSString *repoJSON = [repo modelToJSONString];
    NSLog(@&quot;Repo: %@&quot;, repoJSON);
}
</code></pre><p>&#x8FD9;&#x91CC;&#x901A;&#x8FC7;&#x8C03;&#x7528; modelwithjson&#x65B9;&#x6CD5;&#x6765;&#x8FDB;&#x884C;json&#xFF0D;model&#x7684;&#x8F6C;&#x6362;&#xFF0C;&#x70B9;&#x51FB;&#x65B9;&#x6CD5;&#x8DF3;&#x8FDB;&#x53BB;&#x3002;</p>
<h2 id="instancetype-modelWithJSON-id-json"><a href="#instancetype-modelWithJSON-id-json" class="headerlink" title="+(instancetype)modelWithJSON:(id)json"></a>+(instancetype)modelWithJSON:(id)json</h2><pre><code>+ (instancetype)modelWithJSON:(id)json {
    NSDictionary *dic = [self _yy_dictionaryWithJSON:json];
    return [self modelWithDictionary:dic];
}
</code></pre><p>&#x8FD9;&#x91CC;&#x8C03;&#x7528;&#x4E86;&#x4E00;&#x4E2A;_yy_dictionaryWithJSON&#x65B9;&#x6CD5;&#x8FDB;&#x884C;json&#xFF0D;nsdictionary&#x7684;&#x8F6C;&#x6362;&#x3002;&#x70B9;&#x8FDB;&#x53BB;&#x770B;&#x4E00;&#x770B;</p>
<h2 id="NSDictionary-yy-dictionaryWithJSON-id-json"><a href="#NSDictionary-yy-dictionaryWithJSON-id-json" class="headerlink" title="+(NSDictionary *)_yy_dictionaryWithJSON:(id)json"></a>+(NSDictionary *)_yy_dictionaryWithJSON:(id)json</h2><pre><code>+ (NSDictionary *)_yy_dictionaryWithJSON:(id)json {
    if (!json || json == (id)kCFNull) return nil;
    NSDictionary *dic = nil;
    NSData *jsonData = nil;
    if ([json isKindOfClass:[NSDictionary class]]) {
        dic = json;
    } else if ([json isKindOfClass:[NSString class]]) {
        jsonData = [(NSString *)json dataUsingEncoding : NSUTF8StringEncoding];
    } else if ([json isKindOfClass:[NSData class]]) {
        jsonData = json;
    }
    if (jsonData) {
        dic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
        if (![dic isKindOfClass:[NSDictionary class]]) dic = nil;
    }
    return dic;
}
</code></pre><p>&#x7B2C;&#x4E00;&#x884C;&#x5224;&#x65AD; json&#x662F;&#x5426;&#x4E3A;&#x7A7A;&#xFF0C;kCFNull &#x662F; NSNull &#x7684;&#x5355;&#x4F8B;,&#x5177;&#x4F53;&#x7684;&#x4ECB;&#x7ECD;&#x8BF7;&#x770B;<a href="http://blog.csdn.net/xunyn/article/details/8283653" target="_blank" rel="external">&#x672C;&#x6587;</a>&#x3002;&#x7136;&#x540E;&#x58F0;&#x660E;&#x4E86;NSDictionary&#x548C;NSData&#x4E24;&#x4E2A;&#x7C7B;&#xFF0C; &#x7136;&#x540E;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x5224;&#x65AD;&#xFF0C;&#x4F7F;&#x7528;oc&#x81EA;&#x5E26;&#x7684;NSJSONSerialization&#x6765;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x7684;&#x8F6C;&#x6362;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;NSDictionary&#x7C7B;&#x578B;&#x3002;</p>
<h2 id="instancetype-modelWithDictionary-NSDictionary-dictionary"><a href="#instancetype-modelWithDictionary-NSDictionary-dictionary" class="headerlink" title="+(instancetype)modelWithDictionary(NSDictionary *)dictionary"></a>+(instancetype)modelWithDictionary(NSDictionary *)dictionary</h2><pre><code>+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary {
    if (!dictionary || dictionary == (id)kCFNull) return nil;
    if (![dictionary isKindOfClass:[NSDictionary class]]) return nil;

    Class cls = [self class];
    _YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls];
    if (modelMeta-&gt;_hasCustomClassFromDictionary) {
        cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
    }

    NSObject *one = [cls new];
    if ([one modelSetWithDictionary:dictionary]) return one;
    return nil;
}
</code></pre><p>&#x7EC8;&#x4E8E;&#x5230;&#x4E86;&#x5173;&#x952E;&#x4E00;&#x6B65;&#xFF0C;&#x8FD9;&#x91CC;&#x9996;&#x5148;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x7684;&#x5224;&#x65AD;&#x662F;&#x5426;&#x4E3A;&#x7A7A;&#xFF0C;&#x518D;yymodel&#x91CC;&#x5305;&#x542B;&#x4E86;&#x5F88;&#x591A;&#x7C7B;&#x578B;&#x5224;&#x65AD;&#x548C;&#x7A7A;&#x503C;&#x5224;&#x65AD;&#xFF0C;&#xFF08;&#x770B;&#x6765;&#x4E00;&#x4E2A;&#x6846;&#x67B6;&#x8981;&#x8003;&#x8651;&#x5065;&#x58EE;&#x6027;&#xFF0C;&#x8FD8;&#x6709;&#x6781;&#x5F3A;&#x7684;&#x5BB9;&#x9519;&#x6027;&#x3002;&#xFF09;<br>&#x7C7B;&#x58F0;&#x660E;class cls = [self class]&#x8FD9;&#x91CC;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x6211;&#x4EEC;&#x81EA;&#x5B9A;&#x7684;&#x7C7B;&#xFF08;&#x4E5F;&#x5C31;&#x662F;YYBook&#x7C7B;),&#x7136;&#x540E;&#x4F7F;&#x7528;_YYModelMeta&#x7C7B;,&#x8FD9;&#x4E2A;&#x7C7B;&#x662F;&#x5E72;&#x561B;&#x7528;&#x7684;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x770B;&#x8FD9;&#x4E2A;&#x7C7B;</p>
<h3 id="YYModelMeta"><a href="#YYModelMeta" class="headerlink" title="_YYModelMeta"></a>_YYModelMeta</h3><pre><code>@interface _YYModelMeta : NSObject {
    @package
    YYClassInfo *_classInfo;
    /// Key:mapped key and key path, Value:_YYModelPropertyMeta.
    NSDictionary *_mapper;
    /// Array&lt;_YYModelPropertyMeta&gt;, all property meta of this model.
    NSArray *_allPropertyMetas;
    /// Array&lt;_YYModelPropertyMeta&gt;, property meta which is mapped to a key path.
    NSArray *_keyPathPropertyMetas;
    /// Array&lt;_YYModelPropertyMeta&gt;, property meta which is mapped to multi keys.
    NSArray *_multiKeysPropertyMetas;
    /// The number of mapped key (and key path), same to _mapper.count.
    NSUInteger _keyMappedCount;
    /// Model class type.
    YYEncodingNSType _nsType;

    BOOL _hasCustomWillTransformFromDictionary;
    BOOL _hasCustomTransformFromDictionary;
    BOOL _hasCustomTransformToDictionary;
    BOOL _hasCustomClassFromDictionary;
}
@end
</code></pre><p>&#x5148;&#x4ECE;&#x53C2;&#x6570;&#x8BF4;&#x8D77;</p>
<h3 id="YYClassInfo-classInfo"><a href="#YYClassInfo-classInfo" class="headerlink" title="YYClassInfo *classInfo"></a>YYClassInfo *classInfo</h3><p>&#x8FD9;&#x4E2A;&#x7C7B;&#x662F;&#x4EC0;&#x4E48;&#x610F;&#x601D;&#x5462;&#xFF1F;&#x6765;&#x770B;&#x4E00;&#x4E0B;</p>
<pre><code>@interface YYClassInfo : NSObject
@property (nonatomic, assign, readonly) Class cls; ///&lt; class object
@property (nullable, nonatomic, assign, readonly) Class superCls; ///&lt; super class object
@property (nullable, nonatomic, assign, readonly) Class metaCls;  ///&lt; class&apos;s meta class object
@property (nonatomic, readonly) BOOL isMeta; ///&lt; whether this class is meta class
@property (nonatomic, strong, readonly) NSString *name; ///&lt; class name
@property (nullable, nonatomic, strong, readonly) YYClassInfo *superClassInfo; ///&lt; super class&apos;s class info
@property (nullable, nonatomic, strong, readonly) NSDictionary&lt;NSString *, YYClassIvarInfo *&gt; *ivarInfos; ///&lt; ivars
@property (nullable, nonatomic, strong, readonly) NSDictionary&lt;NSString *, YYClassMethodInfo *&gt; *methodInfos; ///&lt; methods
@property (nullable, nonatomic, strong, readonly) NSDictionary&lt;NSString *, YYClassPropertyInfo *&gt; *propertyInfos; ///&lt; properties
@end
</code></pre><p>&#x7B2C;&#x4E00;&#x4E2A;cls &#x6307;&#x5411;&#x81EA;&#x5DF1;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;superCls&#x6307;&#x5411;&#x81EA;&#x5DF1;&#x7684;&#x7236;&#x7C7B;&#xFF0C;&#x7B2C;&#x4E09;&#x4E2A;&#x7C7B;metaCls&#x6307;&#x5411;&#x81EA;&#x5DF1;&#x7684;&#x5143;&#x7C7B;&#x3002;&#xFF08;&#x5982;&#x679C;&#x8FD9;&#x91CC;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x9700;&#x8981;&#x4E86;&#x89E3;&#x5230;<a href="https://www.ianisme.com/ios/2019.html" target="_blank" rel="external">runtime</a>,&#x4EE5;&#x53CA;<a href="http://www.jianshu.com/p/41735c66dccb" target="_blank" rel="external">&#x7C7B;&#x7684;&#x7684;&#x5185;&#x90E8;&#x7ED3;&#x6784;</a>&#x3002;&#xFF09;&#x8FD9;&#x91CC;&#x662F;&#x5143;&#x7C7B;&#xFF08;metaClass&#xFF09;&#x548C;Class&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x8BE6;&#x7EC6;&#x7684;&#x7C7B;&#x4ECB;&#x7ECD;&#x8BF7;&#x53C2;&#x8003;<a href="http://blog.csdn.net/windyitian/article/details/19810875" target="_blank" rel="external">&#x8FD9;&#x91CC;</a><br><img src="http://images.cnitblog.com/blog/292994/201303/06190150-6424732a31c74ceaaf1c4077b3b9b7b9.png" alt=""></p>
<p>isMeta&#x5224;&#x65AD;&#x81EA;&#x5DF1;&#x662F;&#x5426;&#x662F;&#x5143;&#x7C7B;&#xFF0C;name&#x4E3A;&#x7C7B;&#x7684;&#x540D;&#x79F0;&#xFF0C;YYClassInfo&#x662F;&#x5BF9;&#x81EA;&#x5DF1;&#x7684;&#x7236;&#x7C7B;&#x7684;&#x4E00;&#x4E2A;&#x5C01;&#x88C5;&#xFF0C;&#x5269;&#x4E0B;&#x7684;&#x4E09;&#x4E2A;NSDictionary :ivarInfos,methodInfos,propertyInfos&#x662F;&#x5BF9;&#x7C7B;&#x4E2D;&#x5C5E;&#x6027;(property)&#xFF0C;&#x65B9;&#x6CD5;(method)&#xFF0C;ivar(class)&#x7684;&#x4FDD;&#x5B58;&#x3002;</p>
<h3 id="&#x5176;&#x4ED6;&#x53C2;&#x6570;"><a href="#&#x5176;&#x4ED6;&#x53C2;&#x6570;" class="headerlink" title="&#x5176;&#x4ED6;&#x53C2;&#x6570;"></a>&#x5176;&#x4ED6;&#x53C2;&#x6570;</h3><p>_mapper:&#x5B58;&#x50A8; Json key &#x4E0E; _PropertyMeta&#x5BF9;&#x8C61; &#x7684;&#x6620;&#x5C04;&#x5173;&#x7CFB;&#x5B57;&#x5178;<br>_allPropertyMetas:&#x6570;&#x7EC4;&#x5B58;&#x653E;&#x6240;&#x6709;&#x7684; PropertyMeta<br>_keyPathPropertyMetas:&#x6570;&#x7EC4;&#x5B58;&#x653E;&#x6240;&#x6709;&#x6620;&#x5C04;json keyPath &#x7684; _PropertyMeta&#x5BF9;&#x8C61;<br>_multiKeysPropertyMetas&#xFF1A;&#x6570;&#x7EC4;&#x5B58;&#x653E;&#x6240;&#x6709;&#x6620;&#x5C04;&#x591A;&#x4E2A;json key &#x7684; _PropertyMeta&#x5BF9;&#x8C61;&#xFF08;&#x4E00;&#x5BF9;&#x591A;&#xFF09;</p>
<p>NSUInteger _keyMappedCount; &#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x7684;&#x5C5E;&#x6027;&#x4E2A;&#x6570;&#xFF08;&#x6BD4;&#x5982;YYBook&#x91CC;&#x9762;&#x6709;&#x4E09;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x5982;&#x679C;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x4E2D;&#x5305;&#x542B;&#x53E6;&#x5916;&#x7684;&#x7C7B;B&#x7684;&#x8BDD;&#xFF0C;&#x7C7B;B&#x4EE5;&#x53CA;&#x5176;&#x4E2D;&#x7684;&#x5C5E;&#x6027;&#x4E0D;&#x7B97;&#x5165;&#x5176;&#x4E2D;&#xFF09;<br>_nsType&#xFF1A;&#x89E3;&#x6790;Class&#x662F;Foundation&#x7C7B;&#x578B;&#x54EA;&#x4E00;&#x79CD;<br>_hasCustomWillTransformFromDictionary&#x3001;_hasCustomTransformFromDictionary&#x3001;_hasCustomTransformToDictionary&#x3001;_hasCustomClassFromDictionary&#xFF0C;&#x4F5C;&#x7528;&#xFF1A;&#x5224;&#x65AD;YYModel&#x534F;&#x8BAE;&#x65B9;&#x6CD5;&#x662F;&#x5426;&#x5B9E;&#x73B0;<br>&#x6211;&#x4EEC;&#x5148;&#x6839;&#x636E;&#x6D41;&#x7A0B; &#x9047;&#x5230;&#x54EA;&#x4E2A;&#x53C2;&#x6570;&#x518D;&#x8BB2;&#x3002;</p>
<h2 id="instancetype-modelWithDictionary-NSDictionary-dictionary-1"><a href="#instancetype-modelWithDictionary-NSDictionary-dictionary-1" class="headerlink" title="+(instancetype)modelWithDictionary(NSDictionary *)dictionary"></a>+(instancetype)modelWithDictionary(NSDictionary *)dictionary</h2><p>&#x5148;&#x56DE;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x6B65;&#x9AA4;&#x4E2D;&#x53BB;</p>
<pre><code>+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary {
    if (!dictionary || dictionary == (id)kCFNull) return nil;
    if (![dictionary isKindOfClass:[NSDictionary class]]) return nil;
    Class cls = [self class];
    _YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls];
    if (modelMeta-&gt;_hasCustomClassFromDictionary) {
        cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
    }
    NSObject *one = [cls new];
    if ([one modelSetWithDictionary:dictionary]) return one;
    return nil;
}
</code></pre><p>_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls], &#x5BF9;&#x4E00;&#x4E2A;_YYModelMeta&#x7C7B;&#x8FDB;&#x884C;&#x4E86;&#x5904;&#x7406;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x8DF3;&#x8FDB;&#x53BB;&#x770B;&#x4E00;&#x4E0B;metaWithClass&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;.</p>
<h2 id="instancetype-metaWithClass-Class-cls"><a href="#instancetype-metaWithClass-Class-cls" class="headerlink" title="+ (instancetype)metaWithClass:(Class)cls"></a>+ (instancetype)metaWithClass:(Class)cls</h2><pre><code>+ (instancetype)metaWithClass:(Class)cls {
    if (!cls) return nil;
    static CFMutableDictionaryRef cache;
    static dispatch_once_t onceToken;
    static dispatch_semaphore_t lock;
    dispatch_once(&amp;onceToken, ^{
        cache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &amp;kCFTypeDictionaryKeyCallBacks, &amp;kCFTypeDictionaryValueCallBacks);
        lock = dispatch_semaphore_create(1);
    });
    dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
    _YYModelMeta *meta = CFDictionaryGetValue(cache, (__bridge const void *)(cls));
   NSDictionary *obj = CFDictionaryGetValue(cache, (__bridge const void *)(cls));
    NSLog(@&quot;obj -- %@&quot;,obj);
    NSLog(@&quot;cache - %@&quot;,cache);
    dispatch_semaphore_signal(lock);
    if (!meta ) {
        meta = [[_YYModelMeta alloc] initWithClass:cls];
        if (meta) {
            dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
            CFDictionarySetValue(cache, (__bridge const void *)(cls), (__bridge const void *)(meta));
            dispatch_semaphore_signal(lock);
        }
    }
    return meta;
}
</code></pre><p>&#x8FD9;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x5F00;&#x59CB;&#x6D89;&#x53CA;&#x5230;&#x5E95;&#x5C42;&#x7684;&#x4E00;&#x4E9B;&#x4EE3;&#x7801;&#xFF0C;&#x5176;&#x4E2D;&#x6709;cf&#x6846;&#x67B6;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x7EC4;c&#x8BED;&#x8A00;&#x63A5;&#x53E3;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x5E95;&#x5C42;&#x7684;c&#x53EF;&#x4EE5;&#x4F7F;&#x5F97;&#x4EE3;&#x7801;&#x7684;&#x8FD0;&#x884C;&#x6548;&#x7387;&#x5927;&#x5927;&#x63D0;&#x9AD8;&#x3002;( YYKit&#x7684;&#x6548;&#x7387;&#x9AD8;&#x4E8E;&#x73B0;&#x5728;&#x5176;&#x4ED6;&#x7684;&#x4E3B;&#x6D41;&#x6846;&#x67B6;&#x3002;&#x6548;&#x7387;&#x5BF9;&#x6BD4;&#x8BF7;&#x770B;<a href="http://blog.ibireme.com/2015/10/23/ios_model_framework_benchmark/" target="_blank" rel="external">&#x8FD9;&#x91CC;</a>)&#x3002;&#x5F00;&#x59CB;&#x4ECD;&#x7136;&#x662F;&#x662F;&#x5426;&#x4E3A;&#x7A7A;&#x7684;&#x5224;&#x65AD;&#xFF0C;&#x7136;&#x540E;&#x51FA;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x4EE5;CF&#x5F00;&#x5934;&#x7684;CFMutableDictionaryRef&#x7684;&#x58F0;&#x660E;&#xFF0C;dispatch_once_t  &#x7EBF;&#x7A0B;&#x7684;&#x4E00;&#x4E2A;&#x5355;&#x4F8B;,dispatch_semaphore_t &#x7EBF;&#x7A0B;&#x7684;&#x9501;(&#x8FD9;&#x91CC;&#x662F;&#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x591A;&#x7EBF;&#x7A0B;&#x9020;&#x6210;&#x7684;&#x6570;&#x636E;&#x9519;&#x8BEF;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x9632;&#x6B62;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x6539;&#x53D8;&#x540C;&#x4E00;&#x6570;&#x636E;)&#x3002;<br>dispatch_once(GCD&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x53EA;&#x6267;&#x884C;&#x4E00;&#x6B21;&#xFF0C;&#x4E4B;&#x540E;&#x4FBF;&#x4E0D;&#x5728;&#x6267;&#x884C;&#xFF0C;&#x60F3;&#x6DF1;&#x5165;&#x4E86;&#x89E3;&#x8BF7;&#x770B;<a href="http://www.dreamingwish.com/article/gcd-guide-dispatch-once-1.html" target="_blank" rel="external">&#x8FD9;&#x91CC;</a>)&#x3002;<br>cache&#xFF08;&#x7F13;&#x5B58;&#xFF09;&#x987E;&#x540D;&#x601D;&#x4E49; &#x662F;&#x7528;&#x6765;&#x5B58;&#x653E;&#x6570;&#x636E;&#x7684;&#xFF0C;&#x4E4B;&#x540E;&#x7684;&#x65B9;&#x6CD5;CFDictionaryCreateMutable&#x65B9;&#x6CD5;&#x4E3A;cache&#x7533;&#x8BF7;&#x4E86;&#x4E00;&#x5757;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x7528;&#x6765;&#x5B58;&#x653E;&#x7C7B;&#x7684;&#x76F8;&#x5173;&#x6570;&#x636E;&#x3002;lock&#x89E3;&#x9501;&#x3002;&#xFF08;&#x60F3;&#x4E86;&#x89E3;CFMutableDictionaryRef&#x7684;&#x540C;&#x5B66;&#x8BF7;&#x770B;<a href="http://www.jianshu.com/p/45a974fafa7c" target="_blank" rel="external">&#x8FD9;&#x91CC;</a>&#xFF09;<br>&#x56E0;&#x4E3A;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528;metaWithClass:(class)cls&#x7684;&#x65F6;&#x5019;cls&#x8F6C;&#x6362;meta&#x4E3A;nil&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x8FDB;&#x5165;&#x5230;644&#x884C;meta = [[_YYModelMeta alloc] initWithClass:cls];</p>
<h2 id="instancetype-initWithClass-Class-cls"><a href="#instancetype-initWithClass-Class-cls" class="headerlink" title="-(instancetype)initWithClass:(Class)cls"></a>-(instancetype)initWithClass:(Class)cls</h2><p>&#x8DF3;&#x8FDB;-(instancetype)initWithClass:(Class)cls &#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#xFF0C;&#x5367;&#x69FD;&#xFF0C;200&#x591A;&#x884C;&#x4EE3;&#x7801;&#x5728;&#x7B49;&#x7740;&#x4F60;&#xFF0C;&#x5F88;&#x5F3A;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x4ECE;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E00;&#x884C;&#x5F00;&#x59CB;&#xFF08;&#x4EE3;&#x7801;&#x597D;&#x957F;&#x6211;&#x5C31;&#x4E0D;&#x6574;&#x4E2A;&#x8D34;&#x4E86;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x6253;&#x5F00;xcode&#x81EA;&#x5DF1;&#x67E5;&#x770B;&#xFF09;&#xFF0C;</p>
<ul>
<li><p>(instancetype)classInfoWithClass:(Class)cls  &#x7B2C;&#x4E00;&#x884C;,&#x5367;&#x69FD;&#x53C8;&#x662F;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x597D; &#x518D;&#x8DF3;&#x3002;</p>
<h2 id="instancetype-classInfoWithClass-Class-cls"><a href="#instancetype-classInfoWithClass-Class-cls" class="headerlink" title="+ (instancetype)classInfoWithClass:(Class)cls"></a>+ (instancetype)classInfoWithClass:(Class)cls</h2><ul>
<li>(instancetype)classInfoWithClass:(Class)cls {<br>  if (!cls) return nil;<br>  static CFMutableDictionaryRef classCache;<br>  static CFMutableDictionaryRef metaCache;<br>  static dispatch_once_t onceToken;<br>  static dispatch_semaphore_t lock;<br>  dispatch_once(&amp;onceToken, ^{<pre><code>classCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &amp;kCFTypeDictionaryKeyCallBacks, &amp;kCFTypeDictionaryValueCallBacks);
metaCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &amp;kCFTypeDictionaryKeyCallBacks, &amp;kCFTypeDictionaryValueCallBacks);
lock = dispatch_semaphore_create(1);
</code></pre>  });<br>  dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);<br>  NSLog(@&#x201D;is meta %i&#x201D;,class_isMetaClass(cls));<br>  YYClassInfo <em>info = CFDictionaryGetValue(class_isMetaClass(cls) ? metaCache : classCache, (__bridge const void </em>)(cls));<br>  if (info &amp;&amp; info-&gt;_needUpdate) {<pre><code>[info _update];
</code></pre>  }<br>  dispatch_semaphore_signal(lock);<br>  if (!info) {<pre><code>info = [[YYClassInfo alloc] initWithClass:cls];
if (info) {
    dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
    CFDictionarySetValue(info.isMeta ? metaCache : classCache, (__bridge const void *)(cls), (__bridge const void *)(info));
    dispatch_semaphore_signal(lock);
}
</code></pre>  }<br>  return info;<br>}</li>
</ul>
</li>
</ul>
<p>&#x6709;&#x4E86;&#x524D;&#x9762;&#x7684;&#x5206;&#x6790;&#xFF0C;&#x5BF9;&#x4E8E;&#x8FD9;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;&#x662F;&#x4E0D;&#x662F;&#x89C9;&#x5F97;&#x5F88;&#x5BB9;&#x6613;&#x4E0A;&#x624B;&#xFF1F;<br>&#x672A;&#x5B8C;&#x5F85;&#x7EED;&#x3002;&#x3002;</p>
<p>&#x5E7F;&#x5EA6;&#x51B3;&#x5B9A;&#x4E86;&#x6DF1;&#x5EA6;&#xFF0C;&#x8FD9;&#x53E5;&#x8BDD;&#x5E94;&#x8BE5;&#x662F;&#x6CA1;&#x9519;&#x7684;&#xFF0C;&#x8D8A;&#x6DF1;&#x5C42;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x5C31;&#x8D8A;&#x9700;&#x8981;&#x4F60;&#x6709;&#x5F88;&#x5E7F;&#x7684;&#x77E5;&#x8BC6;&#x9762;&#x624D;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#xFF0C;&#x5C31;&#x90A3;&#x8FD9;&#x7BC7;&#x535A;&#x5BA2;&#x6765;&#x8BF4;&#xFF0C;&#x4E00;&#x4E2A;yymodel&#x6D89;&#x53CA;&#x5230;&#x4E86;&#x5F88;&#x591A;&#x77E5;&#x8BC6;&#x70B9;&#xFF0C;&#x76F8;&#x4FE1;&#x5206;&#x6790;&#x5B8C;&#x6BD5;&#x4E4B;&#x540E;&#xFF0C;&#x5BF9;&#x5176;&#x4ED6;&#x7684;&#x77E5;&#x8BC6;&#x4E5F;&#x80FD;&#x5FEB;&#x901F;&#x638C;&#x63E1;&#xFF0C;&#x6CA1;&#x9519;&#xFF0C;&#x4E00;&#x4E2A;&#x4E00;&#x4E2A;&#x6765;&#xFF1D; &#xFF1D;&#x3002;<br>&#xFF08;YYModel &#x2014; &#x9AD8;&#x6027;&#x80FD;&#x7684; iOS JSON &#x6A21;&#x578B;&#x6846;&#x67B6;&#x3002;<br>YYCache &#x2014; &#x9AD8;&#x6027;&#x80FD;&#x7684; iOS &#x7F13;&#x5B58;&#x6846;&#x67B6;&#x3002;<br>YYImage &#x2014; &#x529F;&#x80FD;&#x5F3A;&#x5927;&#x7684; iOS &#x56FE;&#x50CF;&#x6846;&#x67B6;&#x3002;<br>YYWebImage &#x2014; &#x9AD8;&#x6027;&#x80FD;&#x7684; iOS &#x5F02;&#x6B65;&#x56FE;&#x50CF;&#x52A0;&#x8F7D;&#x6846;&#x67B6;&#x3002;<br>YYText &#x2014; &#x529F;&#x80FD;&#x5F3A;&#x5927;&#x7684; iOS &#x5BCC;&#x6587;&#x672C;&#x6846;&#x67B6;&#x3002;<br>YYKeyboardManager &#x2014; iOS &#x952E;&#x76D8;&#x76D1;&#x542C;&#x7BA1;&#x7406;&#x5DE5;&#x5177;&#x3002;<br>YYDispatchQueuePool &#x2014; iOS &#x5168;&#x5C40;&#x5E76;&#x53D1;&#x961F;&#x5217;&#x7BA1;&#x7406;&#x5DE5;&#x5177;&#x3002;<br>YYAsyncLayer &#x2014; iOS &#x5F02;&#x6B65;&#x7ED8;&#x5236;&#x4E0E;&#x663E;&#x793A;&#x7684;&#x5DE5;&#x5177;&#x3002;<br>YYCategories &#x2014; &#x529F;&#x80FD;&#x4E30;&#x5BCC;&#x7684; Category &#x7C7B;&#x578B;&#x5DE5;&#x5177;&#x5E93;&#x3002;&#xFF09;</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/09/28/yymodel/" rel="prev" title="YYModel解析（2）">
                YYModel解析（2） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Motom" />
          <p class="site-author-name" itemprop="name">Motom</p>
          <p class="site-description motion-element" itemprop="description">Hi! 欢迎来到我的博客，希望这里的知识能够帮助到你！</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">3</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#part-one"><span class="nav-number">2.</span> <span class="nav-text">part one</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#static-void-SimpleObjectExample"><span class="nav-number">2.1.</span> <span class="nav-text">static void SimpleObjectExample</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instancetype-modelWithJSON-id-json"><span class="nav-number">2.2.</span> <span class="nav-text">+(instancetype)modelWithJSON:(id)json</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#NSDictionary-yy-dictionaryWithJSON-id-json"><span class="nav-number">2.3.</span> <span class="nav-text">+(NSDictionary *)_yy_dictionaryWithJSON:(id)json</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instancetype-modelWithDictionary-NSDictionary-dictionary"><span class="nav-number">2.4.</span> <span class="nav-text">+(instancetype)modelWithDictionary(NSDictionary *)dictionary</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#YYModelMeta"><span class="nav-number">2.4.1.</span> <span class="nav-text">_YYModelMeta</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#YYClassInfo-classInfo"><span class="nav-number">2.4.2.</span> <span class="nav-text">YYClassInfo *classInfo</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#其他参数"><span class="nav-number">2.4.3.</span> <span class="nav-text">其他参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instancetype-modelWithDictionary-NSDictionary-dictionary-1"><span class="nav-number">2.5.</span> <span class="nav-text">+(instancetype)modelWithDictionary(NSDictionary *)dictionary</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instancetype-metaWithClass-Class-cls"><span class="nav-number">2.6.</span> <span class="nav-text">+ (instancetype)metaWithClass:(Class)cls</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instancetype-initWithClass-Class-cls"><span class="nav-number">2.7.</span> <span class="nav-text">-(instancetype)initWithClass:(Class)cls</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instancetype-classInfoWithClass-Class-cls"><span class="nav-number">2.8.</span> <span class="nav-text">+ (instancetype)classInfoWithClass:(Class)cls</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Motom</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  

  

  

</body>
</html>
